<?php

namespace app\controllers;

use app\models\Users;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\EntryForm;
use app\models\Goods;
use yii\data\Pagination;

class DemoController extends Controller
{
    //渲染添加页面
    public function actionAdd()
    {
        return $this->render("add");
    }
    //注册数据处理
    public function actionAdddo()
    {
        //接值
        $data = Yii::$app->request->post();
//        print_r($data);
        //判断用户名非空
        if ($data['user_name'] == "")
        {
            die("<script>alert('用户名不能为空');location='?r=demo/add'</script>");
        }
        //判断密码非空
        if ($data['user_pwd'] == "")
        {
            die("<script>alert('密码不能为空');location='?r=demo/add'</script>");
        }
        //判断两次密码是否相同
        if ($data['user_pwd'] != $data['user_repwd'])
        {
            die("<script>alert('两次密码不符合');location='?r=demo/add'</script>");
        }
        $str=rand(9999999,99999999);
        //实例化
        $user =new Users();
        $user->user_name=$data['user_name'];
        $user->user_pwd=$data['user_pwd'];
//        $user->user_number=$str;
        //利用model层 进行添加数据
        $res=$user->addData();
        //判断
        if ($res){
            //成功跳转到登录页面
            return $this->redirect(['demo/login']);
        }else{
            //失败
            echo "注册失败";
            die();
        }

    }

    //渲染登录页面
    public function actionLogin()
    {
        return $this->render("login");
    }
    //登录数据处理
    public function actionLogindo()
    {
        //接值
        $data = Yii::$app->request->post();
//        print_r($data);
        if ($data['user_name'] == "")
        {
            die("<script>alert('用户名不能为空');location='?r=demo/login'</script>");
        }
        //判断密码非空
        if ($data['user_pwd'] == "")
        {
            die("<script>alert('密码不能为空');location='?r=demo/login'</script>");
        }
        //实例化
        $user =new Users();
        $user->user_name=$data['user_name'];
        $user->user_pwd=$data['user_pwd'];
        //调用model层进行对数据的查询
        $res=$user->findData($data);
//        print_r($res);die();
        if ($res){
            //成功跳转到登录页面
            return $this->redirect(['demo/show']);
        }else{
            //失败
            echo "注册失败";
            die();
        }

    }
    //分页展示页面
    public function actionShow()
    {
        //前台搜索 接值
        $name=Yii::$app->request->post('val');

        //查询语句
        $query = Goods::find()
            ->select('*')
            ->innerjoin("brand","goods.brand_id=brand.brand_id")
            ->innerjoin("type","goods.type_id=type.type_id");

        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(),'defaultPageSize' => 5]);
        $models = $query->offset($pages->offset)
        ->limit($pages->limit)
        ->asArray()
        ->all();

        //渲染 传值
        return $this->render('show', [
            'models' => $models,
            'pages' => $pages,
        ]);
    }
    //详情页
    public function actionInfo()
    {
        //接值
        $id=$_GET['id'];
        //实例化商品表
        $goods = new Goods();
        //调用商品表model 查询数据 再详情页展示
        $data=$goods->findData($id);
//        print_r($data);
        //传值 渲染页面
        return $this->render("info",['data'=>$data]);
    }
}



?>